<?php
// +----------------------------------------------------------------------
// | 添加修改用户信息模型
// +----------------------------------------------------------------------
// | @link
// +----------------------------------------------------------------------
// | @copyright
// +----------------------------------------------------------------------
// | @licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | @author Haijun Wu <nicholasinlove@126.com>
// +----------------------------------------------------------------------
// | $Id: AdminUserModel.class.php 109 2011-05-06 08:21:00Z nicholasinlove1986@gmail.com $
// +----------------------------------------------------------------------


class AdminUserModel extends Model {

	public $_validate	=	array(
		array('user_name','/^[a-z][A-Za-z0-9_]\w{1,19}$/i','{%user_name_require}'),
		array('user_nickname','require','{%user_nickname_require}'),
		array('user_email','email','{%user_email_error}'),
		array('user_password','require','{%user_password_require}'),
		array('pwd_confirm','require','{%pwd_confirm_require}'),
		array('user_status','require','{%user_status_}'),
		array('pwd_confirm','user_password','{%pwd_confirm_}',0,'confirm'),
		array('user_name','','{%user_name_unique}',0,'unique',1),
		array('user_nickname','','{%user_nickname_unique}',0,'unique',1),
	);

	public $_auto		=	array(
		array('user_password','phpass',3,'callback'),
		array('user_nickname','replaceUserNickName',3,'callback'),
		array('user_status','1',1),
		array('user_level','1',1),
	);
	
	/**
	 * 处理用户密码,Phpass加密哦
	 *
	 */	
	protected function phpass() {
		if (isset($_POST['user_password'])) {
			import("@.ORG.Phpass");
			$nb_hasher = new PasswordHash(8, true);	
			return $nb_hasher->HashPassword($_POST['pwd_confirm']);
		} else {
			return false;
		}
	}
	
	/**
	 * 处理用户昵称
	 *
	 */
	protected function replaceUserNickName() {
		$str  =  $_POST['user_nickname'];
		$str  =  str_replace('，', ',', $str);
		$str  =  str_replace(',,', ',', $str);
		return $str;
	}
	
	/**
	 * 获取用户昵称
	 *
	 * @access  public
	 * @param   string  $user_id    用户id
	 * @param   string  $selected   当前选中用户昵称
	 * @param   boolean $re_type    返回的类型: 值为真时返回下拉列表,否则返回数组
	 * @return  array
	 */
	public function getUserNickName($re_type = true, $selected = '' , $user_id) {
	    if (!isset($user_id))  $user_id = $_SESSION[C('USER_AUTH_KEY')];
		$arr = $this->field('user_nickname')->where("user_id=$user_id")->find();
		foreach ($arr as $name) {
			$newarr = explode(',',$name);
		}
		if ($re_type) {
			foreach ($newarr as $s) {
				$select .= "<option value='{$s}' ";
				$select .= ($s == $selected) ? "selected='selected'" : '';
				$select .= ">";
				$select .= "{$s}</option>";	
			}			
			return $select;
		} else {		
			return $newarr;
        }
	}
	
}

?>